/**
 * 
 */
package com.tiduyun.data.exchange.util.http.config;

import java.util.List;

import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;

import lombok.Data;

/**
 * @author wuzheng@tiduyun.com
 *
 */
@Data
public class CmpHttpClientRequestConfigProperties {

    private Boolean expectContinueEnabled;

    private CmpHttpClientProxyProperties proxy;

    private String cookieSpec;

    private Boolean redirectsEnabled;

    private Boolean relativeRedirectsAllowed;

    private Boolean circularRedirectsAllowed;

    private Integer maxRedirects;

    private Boolean authenticationEnabled;

    private List<String> targetPreferredAuthSchemes;

    private List<String> proxyPreferredAuthSchemes;

    private Integer connectionRequestTimeout;

    private Integer connectTimeout;

    private Integer socketTimeout;

    private Boolean contentCompressionEnabled;

    public void apply(RequestConfig.Builder requestConfigBuilder) {
        if (expectContinueEnabled != null) {
            requestConfigBuilder.setExpectContinueEnabled(expectContinueEnabled);
        }
        if (proxy != null && "http".equalsIgnoreCase(proxy.getType())) {
            HttpHost proxyHost = new HttpHost(proxy.getHost(), proxy.getPort());
            requestConfigBuilder.setProxy(proxyHost);
        }
        if (cookieSpec != null) {
            requestConfigBuilder.setCookieSpec(cookieSpec);
        }
        if (redirectsEnabled != null) {
            requestConfigBuilder.setRedirectsEnabled(redirectsEnabled);
        }
        if (relativeRedirectsAllowed != null) {
            requestConfigBuilder.setRelativeRedirectsAllowed(relativeRedirectsAllowed);
        }
        if (circularRedirectsAllowed != null) {
            requestConfigBuilder.setCircularRedirectsAllowed(circularRedirectsAllowed);
        }
        if (maxRedirects != null) {
            requestConfigBuilder.setMaxRedirects(maxRedirects);
        }
        if (authenticationEnabled != null) {
            requestConfigBuilder.setAuthenticationEnabled(authenticationEnabled);
        }
        if (targetPreferredAuthSchemes != null) {
            requestConfigBuilder.setTargetPreferredAuthSchemes(targetPreferredAuthSchemes);
        }
        if (proxyPreferredAuthSchemes != null) {
            requestConfigBuilder.setProxyPreferredAuthSchemes(proxyPreferredAuthSchemes);
        }
        if (connectionRequestTimeout != null) {
            requestConfigBuilder.setConnectionRequestTimeout(connectionRequestTimeout);
        }
        if (connectTimeout != null) {
            requestConfigBuilder.setConnectTimeout(connectTimeout);
        }
        if (socketTimeout != null) {
            requestConfigBuilder.setSocketTimeout(socketTimeout);
        }
        if (contentCompressionEnabled != null) {
            requestConfigBuilder.setContentCompressionEnabled(contentCompressionEnabled);
        }
    }

    public RequestConfig build() {
        RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
        apply(requestConfigBuilder);
        return requestConfigBuilder.build();
    }

}
